/*
 * Copyright (C) 2010 M. Hautle.
 * 
 * This file is part of SimpleEL.
 * 
 * SimpleEL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * SimpleEL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with SimpleEL. If not, see <http://www.gnu.org/licenses/>.
 */
package ch.simpleel.util;

/**
 * Object representing an key for an property.<br>
 * You may use this class to speed up lookups (it's faster to create such a class than to do an string concatenation).
 * 
 * @author M. Hautle
 */
public final class PropertyKey {
    /** The type holding the property. */
    private final Class<?> type;

    /** The name of the property. */
    private final String property;

    /** The hash of this key. */
    private final int hash;

    /**
     * Default constructor.
     * 
     * @param type The type holding the property
     * @param property The property name
     */
    public PropertyKey(Class<?> type, String property) {
        this.type = type;
        this.property = property;
        this.hash = 31 * (31 + property.hashCode()) + type.hashCode();
    }

    /**
     * Returns the type holding the property.
     * 
     * @return The type
     */
    public Class<?> getType() {
        return type;
    }

    /**
     * Returns the property name.
     * 
     * @return The name
     */
    public String getProperty() {
        return property;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        return hash;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof PropertyKey))
            return false;
        final PropertyKey o = ((PropertyKey) obj);
        return type.equals(o.type) && property.equals(o.property);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return type.getName() + "." + property;
    }
}